<?php

namespace HyperfTest\Cases\Api;

use App\Model\SmsModel;
use Hyperf\Testing\TestCase;

class UserApiTest extends  TestCase
{

    /**
     * @test
     * @group reg-account
     * @dataProvider registerByAccountProvider
     */
    public function registerByAccount($account, $password)
    {
        $result = $this->post('/register-account', [
            'account' => $account,
            'password' => $password
        ])->assertOk()->getContent();
        var_dump($result);
    }

    /**
     * 账号注册数据供给器
     * @return array
     */
    public static function registerByAccountProvider(){
        $data = [];
        for($i = 0 ; $i < 10; $i++){
            $account = mt_rand(1,9) . mt_rand(1,9) . mt_rand(1,9) . mt_rand(0,999999999);
            $account = str_pad($account,11, '0');
            $password = mt_rand(0,999999999);
            $data[] = [$account, $password];
        }
        return $data;
    }

    /**
     * @group reg-mobile
     * @test
     * @dataProvider registerByMobileProvider
     */
    public function registerByMobile($mobile)
    {
        $result = $this->post('/sms', ['mobile' => $mobile])
            ->assertOk()->getContent();
        $result = json_decode($result, true);
        var_dump($result);
        $this->assertSame($result['code'], 200, "短信发送失败");
        // 模拟获取短信码
        $code = $this->mockSmsCode($mobile);
        $result = $this->post('/register-mobile', [
            'mobile' => $mobile,
            'code' => $code
        ])->assertOk()->getContent();
        var_dump($result);
    }

    protected function mockSmsCode($mobile){
        $code = SmsModel::where('mobile', $mobile)
            ->where('event', 'register')
            ->value('code');
        return $code;
    }

    public static function registerByMobileProvider(){
        $data = [];
        for($i = 0 ; $i < 10; $i++){
            $mobile = mt_rand(1,9) . mt_rand(1,9) . mt_rand(1,9) . mt_rand(0,999999999);
            $data[] = [$mobile];
        }
        return $data;
    }
}